using System;
using Microsoft.SPOT;

namespace Gadgeteer.Modules.Community.XBee
{
    /// <summary>
    /// This class is used on incoming messages to create concrete classes representing the data
    /// </summary>
    public static class APIMessageFactory
    {
        /// <summary>
        /// Parse an XBee Transmit Status message
        /// </summary>
        /// <param name="message">byte array received</param>
        /// <returns>TransmitStatus object</returns>
        public static TransmitStatus CreateTransmitStatus(byte[] message)
        {
            TransmitStatus result = new TransmitStatus();
            result.Parse(message);
            return result;
        }

        /// <summary>
        /// Parse a XBee AT response message
        /// </summary>
        /// <param name="message">byte array received</param>
        /// <returns>ATResponse object</returns>
        public static ATResponse CreateATResponse(byte[] message)
        {
            ATResponse result = null;
            string atCommand = new string(new char[] { Convert.ToChar(message[5]), Convert.ToChar(message[6]) });
            switch (atCommand)
            {
                case "ND":
                    result = new NodeDiscoveryResponse();
                    break;
                default:
                    result = new ATResponse();
                    break;
            }

            result.Parse(message);

            return result;
        }

        /// <summary>
        /// Parse an XBee Transmit Receive message
        /// </summary>
        /// <param name="message">byte array received</param>
        /// <returns>TransmitReceive object</returns>
        public static TransmitReceive CreateTransmitReceive(byte[] message)
        {
            TransmitReceive result = new TransmitReceive();
            result.Parse(message);
            return result;
        }
    }
}
